package com.sl.eurekaserver.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheKey;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheRemove;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult;
import com.sl.eurekaserver.entity.User;

/**
 * 使用注解添加和清除缓存
 * @author 舒莉
 *
 */
@RestController
public class AnnonCacheController {
	
	@Autowired
	private RestTemplate restTemplate;
	

	@CacheResult(cacheKeyMethod="getCacheKey")
	@HystrixCommand
	public User getUser(String userName){
		return restTemplate.getForObject("http://HELLO-SERVICE/hello/{userName}",
				User.class, userName);
	}
	
	public String getCacheKey(String userName){
		return userName;
	}
	
	//@CacheKey 优先级比cacheKeyMethod 低
	@CacheResult
	@HystrixCommand
	public User getUser2(@CacheKey("userName") String userName){
		return restTemplate.getForObject("http://HELLO-SERVICE/hello/{userName}",
				User.class, userName);
	}
	
	//更新操作上清除缓存
	@CacheRemove(commandKey="getUser2")
	@HystrixCommand
	public void update(@CacheKey("userName") User user){
		
	}
	
	
}
